package io.jft.doll.room.repository

import io.jft.doll.api.AddressApi
import io.jft.doll.api.apiProvider
import io.jft.doll.room.database.AppDatabase
import io.jft.doll.room.entity.common.AddressItemCEntity
import com.g.base.extend.runDataBaseTransition
import com.g.base.room.repository.BaseRepository
import com.g.base.room.repository.NetworkBoundResult
import com.google.gson.JsonObject

/**
 * Created by G on 2017/11/13 0013.
 */
class AddressRepository : BaseRepository() {
    fun getRegion(type: String, parent: String) =
            NetworkBoundResult(
                    {
                        apiProvider.create(AddressApi::class.java)
                                .getRegion(JsonObject().apply {
                                    addProperty("type", type)
                                    addProperty("parent", parent)
                                })
                    },
                    {
                        AppDatabase.instant.getAddressDao()
                                .getRegions(type, parent)
                    },
                    saveRemoteResult = {
                        it?.apply {
                            forEach { it.parent = parent; it.type = type }
                            AppDatabase.instant.runDataBaseTransition {
                                getAddressDao()
                                        .insertAllRegion(*it.toTypedArray())
                            }
                        }
                    }
            )

    fun getAllAddress(refresh: Boolean): NetworkBoundResult<List<AddressItemCEntity>> {
        var selectId: String? = null
        return NetworkBoundResult(
                {
                    apiProvider.create(AddressApi::class.java)
                            .getAllAddress()
                },
                {
                    AppDatabase.instant.getAddressDao()
                            .getAll()
                },
                {
                    if (refresh) {
                        selectId = null
                        AppDatabase.instant.runDataBaseTransition {
                            selectId = getAddressDao().getAllSync().firstOrNull{it.isSelect != 0}?.id
                            getAddressDao().nukeAddressTable()
                        }
                    }
                    refresh || it == null || it.isEmpty()
                },
                {
                    it?.apply {
                        AppDatabase.instant.runDataBaseTransition {
                            it.firstOrNull{it.id == selectId}?.isSelect = 1
                            getAddressDao().insertAllAddress(*it.toTypedArray())
                        }
                    }
                }
        )
    }

    fun getSelect() =
            NetworkBoundResult(
                    loadFormDb = {
                        AppDatabase.instant
                                .getAddressDao()
                                .getSelect()
                    }
            )

    fun getDefault() =
            NetworkBoundResult(
                    loadFormDb = {
                        AppDatabase.instant
                                .getAddressDao()
                                .getDefault()
                    }
            )
}